package com.dkd.quartz.task;

import com.dkd.sbtz.domain.AssetAssessment;
import com.dkd.sbtz.domain.AssetMtplan;
import com.dkd.sbtz.mapper.AssetAssessmentMapper;
import com.dkd.sbtz.mapper.AssetMtplanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;

@Component("assetPlanGenTaskBak")
public class AssetPlanGenTaskBak {

    @Autowired
    private AssetAssessmentMapper assessmentMapper;

    @Autowired
    private AssetMtplanMapper mtplanMapper;


    private LocalDate toLocalDate(Date date) {
        return date.toInstant().atZone(java.time.ZoneId.systemDefault()).toLocalDate();
    }
    /**
     * 自动生成一年的维护计划
     */
    public void generateYearPlan() {
        List<AssetAssessment> list = assessmentMapper.selectAllValid();
        System.out.println("找到设备评估数量: " + list.size());

        for (AssetAssessment a : list) {
            int monthCycle = getCycleByLevel(a.getCriticalLevel());
            if (monthCycle == 0) continue;

            // 维护工种（例如: "1,2"）
            String[] trades = a.getMaintainType().split(",");

            LocalDate start = toLocalDate(a.getInitialDate());
            for (String trade : trades) {
                for (int i = 0; i < 12; i += monthCycle) {
                    LocalDate planDate = start.plusMonths(i);
                    if (planDate.isAfter(start.plus(1, ChronoUnit.YEARS))) break;

                    AssetMtplan plan = new AssetMtplan();
                    plan.setAssetId(a.getAssetId());
                    plan.setAssetmentId(a.getId());
                    plan.setTitle("年度维护计划-" + a.getCriticalLevel());
                    plan.setPlanDate(java.sql.Date.valueOf(planDate));
                    plan.setPlanHour(0);
                    plan.setPlanContent(a.getPlanContent());
                    plan.setSpareType(a.getIsSpecial());
                    plan.setNeedSpare(1);
                    plan.setTradeCode(trade.trim());
                    plan.setPriority(getPriorityByLevel(a.getCriticalLevel()));
                    plan.setPlanType("PM");
                    plan.setCycleType("MONTH");
                    plan.setCycleValue(monthCycle);
                    plan.setGenPolicy("AUTO");
                    plan.setStatus("OPEN");
                    plan.setRemark("系统自动生成");
                    plan.setCreateBy("system");
                    plan.setCreateTime(new Date());
                    plan.setIsDelete(0);

                    mtplanMapper.insertAssetMtplanTmp(plan);
                }
            }
        }
        System.out.println("维护计划生成完成 ✅");
    }

    private int getCycleByLevel(String level) {
        if ("A".equalsIgnoreCase(level)) return 1;
        if ("B".equalsIgnoreCase(level)) return 2;
        if ("C".equalsIgnoreCase(level)) return 3;
        return 0;
    }

    private String getPriorityByLevel(String level) {
        if ("A".equalsIgnoreCase(level)) return "HIGH";
        if ("B".equalsIgnoreCase(level)) return "MEDIUM";
        return "LOW";
    }
}
